import {
  apiRelatedCompanyList,
  apiaddRelatedCompany,
  apiUpdateRelatedCompany,
  apiDeleteRelatedCompany,
} from '@/services/api';

export default {
  namespace: 'RelatedCompany',
  state: {
    relatedCompanyData: {},
    updateDidFinish: false,
  },

  effects: {
    *getRelatedCompanyList({ payload }, { call, put }) {
      const response = yield call(apiRelatedCompanyList, payload);
      console.log('获取数据:', response);
      if (response) {
        yield put({
          type: 'setRelatedCompanyData',
          payload: {
            relatedCompanyData: response.data,
          },
        });
      }
    },
    *addRelatedCompany({ payload }, { call, put }) {
      // console.log("payload===",payload)
      const response = yield call(apiaddRelatedCompany, payload);
      console.log('新增公司关联：', response);
      yield put({
        type: 'setPayload',
        payload: {
          updateDidFinish: new Date().getTime(),
        },
      });
    },
    *updateRelatedCompany({ payload }, { call, put }) {
      const response = yield call(apiUpdateRelatedCompany, payload);
      yield put({
        type: 'setPayload',
        payload: {
          updateDidFinish: new Date().getTime(),
        },
      });
    },
    *deleteRelatedCompany({ payload }, { call, put }) {
      console.log('删除id：', payload);
      const response = yield call(apiDeleteRelatedCompany, payload);
      yield put({
        type: 'setPayload',
        payload: {
          updateDidFinish: new Date().getTime(),
        },
      });
      // console.log('获取数据', response);
    },
  },

  reducers: {
    setPayload(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    setRelatedCompanyData(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
